/*
    Space Simulation - Simulate the solar system
    Copyright (C) 2010  Guillaume Andrieu and Sylvain Martin

    This file is part of Space Simulation.

    Space Simulation is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Space Simulation is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Space Simulation.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
 *Vector3D.c est le code source qui contiens toutes le fonctions de calculs geometrique dans l'espace.
 *	MakeVector3D : fonction de creation d'un vecteur a trois composantes (dans l'espace)
 *	VectorCrossProduct : fonction produit vectoriel
 *	VectorSum	   : fonction de somme vectorielle
 *	VectorDotProduct   : fonction produit scalaire
 *	VectorScalarMul	   : fonction de multiplication par un scalaire
 *	VectorNorm	   : fonction de calcul de la norme d'un vecteur
 *	VectorAngle	   : fonction de calcul de l'angle entre deux vecteurs
 *	VectorUnit	   : fonction qui calcule la normalisation d'un vecteur. (vecteur unitaire)
 *	VectorSubstract	   : fonction de soustraction de deux vecteurs
 */


#include <math.h>
#include "Vector3D.h"

Vector3D MakeVector3D(float x, float y, float z)
{
  Vector3D vector;
  vector.x = x;
  vector.y = y;
  vector.z = z;
  return vector;
}

Vector3D VectorCrossProduct(Vector3D v1, Vector3D v2)
{
  Vector3D w;
  w.x = v1.y * v2.z - v1.z * v2.y;
  w.y = v1.z * v2.x - v1.x * v2.z;
  w.z = v1.x * v2.y - v1.y * v2.x;
  return w;
}

Vector3D VectorSum(Vector3D v1, Vector3D v2)
{
  Vector3D v;
  v.x = v1.x + v2.x;
  v.y = v1.y + v2.y;
  v.z = v1.z + v2.z;
  return v;
}

double VectorDotProduct(Vector3D v1, Vector3D v2)
{
  return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

Vector3D VectorScalarMul(double s, Vector3D v)
{
  Vector3D w;
  w.x = s * v.x;
  w.y = s * v.y;
  w.z = s * v.z;
  return w;
}

double VectorNorm(Vector3D v)
{
  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

double VectorAngle(Vector3D v1, Vector3D v2)
{
 Vector3D v1n = VectorUnit(v1);
 Vector3D v2n = VectorUnit(v2);
 return acos( VectorDotProduct(v1n, v2n) );
}

Vector3D VectorUnit(Vector3D v)
{
  return VectorScalarMul(1/VectorNorm(v), v);
}

Vector3D VectorSubstract(Vector3D v1, Vector3D v2)
{
  return VectorSum(v1, VectorScalarMul(-1.0, v2));
}
